package com.zhangyang.esper.pattern;

import com.espertech.esper.client.Configuration;
import com.espertech.esper.client.EPServiceProvider;
import com.espertech.esper.client.EPServiceProviderManager;
import com.espertech.esper.client.EPStatement;
import com.espertech.esper.client.scopetest.EPAssertionUtil;
import com.espertech.esper.client.scopetest.SupportUpdateListener;
import com.espertech.esper.client.time.CurrentTimeEvent;
import com.espertech.esper.client.util.DateTime;
import com.espertech.esper.metrics.instrumentation.InstrumentationHelper;
import junit.framework.TestCase;

import java.math.BigDecimal;


/**
 * @author Dell
 * @Title:
 * @Description: 请写注释类
 * @date 2022/8/23
 */
public class MultiOperator extends TestCase {
    private EPServiceProvider epService;
    private SupportUpdateListener listener;

    public void setUp() {
        Configuration config = new Configuration();
        config.getEngineDefaults().getThreading().setInternalTimerEnabled(false);
//        config.addEventType(SupportBean.class);
        epService = EPServiceProviderManager.getDefaultProvider(config);
        epService.initialize();
        listener = new SupportUpdateListener();
        if (InstrumentationHelper.ENABLED) { InstrumentationHelper.startTest(epService, this.getClass(), getName());}
    }

    public  void testMultipleCompare(){
        epService.getEPAdministrator().createEPL("create objectarray schema TemperatureSensorEvent(id string, device string, temp int)");

        String[] fields = "a_id,b_id".split(",");
//        String epl = "@Audit select * from TemperatureSensorEvent\n" +
//                "match_recognize (\n" +
////                "  partition by device\n" +
//                "  measures A.id as a_id, B.id as b_id\n" +
//                "  pattern (match_recognize_permute(A, B))\n" +
//                "  define \n" +
//                "\tA as A.temp < 100, \n" +
//                "\tB as B.temp >= 100)";
        String epl = "@Audit select * from TemperatureSensorEvent\n" +
                "match_recognize (\n" +
//                "  partition by device\n" +
                "  measures A as a_id, B as b_id, C as c_od\n" +
                "  pattern (match_recognize_permute(A*, B*,C*))\n" +
                "  define \n" +
                "\tA as (A.id = 'E1') , B as (B.id = 'E2'  ), C as (C.id = 'E3'  ) and B.temp>=A.temp and C.temp = A.temp )";
        System.out.println(epl);
        EPStatement stmt = epService.getEPAdministrator().createEPL(epl);
        stmt.addListener(listener);

        epService.getEPRuntime().sendEvent(new Object[] {"E1", "1", 100 }, "TemperatureSensorEvent");
        epService.getEPRuntime().sendEvent(new Object[] {"E1", "1", 100}, "TemperatureSensorEvent");
//        EPAssertionUtil.assertProps(listener.assertOneGetNewAndReset(), fields, new Object[]{"E1", "E2"});

        epService.getEPRuntime().sendEvent(new Object[] {"E1", "1", 100}, "TemperatureSensorEvent");
        epService.getEPRuntime().sendEvent(new Object[] {"E2", "1", 100}, "TemperatureSensorEvent");
//        EPAssertionUtil.assertProps(listener.assertOneGetNewAndReset(), fields, new Object[]{"E4", "E3"});

        epService.getEPRuntime().sendEvent(new Object[] {"E2", "1", 100}, "TemperatureSensorEvent");
        epService.getEPRuntime().sendEvent(new Object[] {"E3", "1", 100}, "TemperatureSensorEvent");
        assertFalse(listener.isInvoked());

        stmt.destroy();
    }

    public  void test2MultipleCompare(){
        epService.getEPAdministrator().createEPL("create objectarray schema TemperatureSensorEvent(id string, device string, temp int)");


//.lastEvents()

        String[] fields = "a_id,b_id".split(",");
//        String epl = "@Audit select * from TemperatureSensorEvent\n" +
//                "match_recognize (\n" +
////                "  partition by device\n" +
//                "  measures A.id as a_id, B.id as b_id\n" +
//                "  pattern (match_recognize_permute(A, B))\n" +
//                "  define \n" +
//                "\tA as A.temp < 100, \n" +
//                "\tB as B.temp >= 100)";
        String epl = "@Audit select * from newStream\n" +
                "match_recognize (\n" +
//                "  partition by id\n" +
                "  measures A as a_id, B as b_id, C as c_od\n" +
                "  pattern (every match_recognize_permute((A, B,C)))\n" +
                "  define \n" +
                "\t A as (A.id = 'E1') , B as (B.id = 'E2'  ), C as (C.id = 'E3'  ) and B.temp>=A.temp and C.temp = A.temp  )";
//        A as A.id = 'E1'
        System.out.println(epl);
        EPStatement stmt = epService.getEPAdministrator().createEPL(epl);
        stmt.addListener(listener);

        epService.getEPRuntime().sendEvent(new Object[] {"E1", "1", 100 }, "TemperatureSensorEvent");
        epService.getEPRuntime().sendEvent(new Object[] {"E1", "1", 100}, "TemperatureSensorEvent");
//        EPAssertionUtil.assertProps(listener.assertOneGetNewAndReset(), fields, new Object[]{"E1", "E2"});

        epService.getEPRuntime().sendEvent(new Object[] {"E1", "1", 100}, "TemperatureSensorEvent");
        epService.getEPRuntime().sendEvent(new Object[] {"E2", "1", 100}, "TemperatureSensorEvent");
//        EPAssertionUtil.assertProps(listener.assertOneGetNewAndReset(), fields, new Object[]{"E4", "E3"});

        epService.getEPRuntime().sendEvent(new Object[] {"E2", "1", 100}, "TemperatureSensorEvent");
        epService.getEPRuntime().sendEvent(new Object[] {"E3", "1", 100}, "TemperatureSensorEvent");
        assertFalse(listener.isInvoked());

        stmt.destroy();
    }


    public  void testTimeMultipleCompare(){
        epService.getEPAdministrator().createEPL("create objectarray schema TemperatureSensorEvent(id string, device string, temp int, time long)");
//        epService.getEPAdministrator().createEPL("insert into newStream select * from TemperatureSensorEvent(id='E1')");
//        epService.getEPAdministrator().createEPL("insert into newStream select * from TemperatureSensorEvent(id='E2')");
//        epService.getEPAdministrator().createEPL("insert into newStream select * from TemperatureSensorEvent(id='E3')");
        String[] fields = "a_id,b_id".split(",");
//        String epl = "@Audit select * from TemperatureSensorEvent\n" +
//                "match_recognize (\n" +
////                "  partition by device\n" +
//                "  measures A.id as a_id, B.id as b_id\n" +
//                "  pattern (match_recognize_permute(A, B))\n" +
//                "  define \n" +
//                "\tA as A.temp < 100, \n" +
//                "\tB as B.temp >= 100)";
        String epl = "@Audit select * from TemperatureSensorEvent.ext:time_order(time, 1 sec)\n" +
                "match_recognize (\n" +
//                "  partition by id\n" +
                "  measures A as a_id, B as b_id, C as c_od\n" +
                "  pattern ( match_recognize_permute(A, B,C))\n" +
                "  define \n" +
                "\t  A as (A.id = 'E1') , B as (B.id = 'E2'  ), C as (C.id = 'E3'  ) and B.temp>=A.temp and C.temp = A.temp   )";
//        A as A.id = 'E1'
        System.out.println(epl);
        EPStatement stmt = epService.getEPAdministrator().createEPL(epl);
        stmt.addListener(listener);
        epService.getEPRuntime().sendEvent(new CurrentTimeEvent(0));
        epService.getEPRuntime().sendEvent(new CurrentTimeEvent(1661335063l));

        epService.getEPRuntime().sendEvent(new Object[] {"E1", "1", 100,1661335063l }, "TemperatureSensorEvent");
        epService.getEPRuntime().sendEvent(new CurrentTimeEvent(1661336064l));
        epService.getEPRuntime().sendEvent(new Object[] {"E1", "1", 100,1661336064l}, "TemperatureSensorEvent");
//        EPAssertionUtil.assertProps(listener.assertOneGetNewAndReset(), fields, new Object[]{"E1", "E2"});
        epService.getEPRuntime().sendEvent(new CurrentTimeEvent(1661338065l));
        epService.getEPRuntime().sendEvent(new Object[] {"E1", "1", 100,1661338065l}, "TemperatureSensorEvent");
        epService.getEPRuntime().sendEvent(new CurrentTimeEvent(1661338067l));
        epService.getEPRuntime().sendEvent(new Object[] {"E2", "1", 100,1661338067l}, "TemperatureSensorEvent");
//        EPAssertionUtil.assertProps(listener.assertOneGetNewAndReset(), fields, new Object[]{"E4", "E3"});
        epService.getEPRuntime().sendEvent(new CurrentTimeEvent(1661338969l));
        epService.getEPRuntime().sendEvent(new Object[] {"E3", "1", 100,1661338969l}, "TemperatureSensorEvent");
        epService.getEPRuntime().sendEvent(new CurrentTimeEvent(1661311965l));
        epService.getEPRuntime().sendEvent(new Object[] {"E2", "1", 100,1661312965l}, "TemperatureSensorEvent");

        assertFalse(listener.isInvoked());

        stmt.destroy();
    }


}
